<template>
  <div class="product-card" @click="goToDetail">
    <div class="product-image">
      <el-image 
        :src="product.main_image" 
        :alt="product.name_en"
        fit="cover"
        lazy
      >
        <template #placeholder>
          <div class="image-placeholder">
            <n-icon size="48" color="var(--color-gray)">
              <ImageOutline />
            </n-icon>
          </div>
        </template>
        <template #error>
          <div class="image-placeholder">
            <n-icon size="48" color="var(--color-gray)">
              <ImageOutline />
            </n-icon>
          </div>
        </template>
      </el-image>
      <div v-if="product.is_hot" class="hot-badge">
        <n-icon size="16"><FlameOutline /></n-icon>
        HOT
      </div>
    </div>
    
    <div class="product-info">
      <h3 class="product-name">{{ product.name_en }}</h3>
      <p class="product-description">{{ truncateText(product.description, 60) }}</p>
      
      <div v-if="product.colors && product.colors.length" class="product-colors">
        <span 
          v-for="(color, index) in product.colors.slice(0, 4)" 
          :key="index"
          class="color-dot"
          :style="{ backgroundColor: color }"
        ></span>
        <span v-if="product.colors.length > 4" class="more-colors">
          +{{ product.colors.length - 4 }}
        </span>
      </div>
      
      <div class="product-footer">
        <span class="product-category">{{ product.category_name_en }}</span>
        <button class="contact-btn" @click.stop="contactNow">
          <n-icon size="18"><ChatbubbleEllipsesOutline /></n-icon>
          Contact
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { NIcon } from 'naive-ui'
import { ImageOutline, FlameOutline, ChatbubbleEllipsesOutline } from '@vicons/ionicons5'
import { useRouter } from 'vue-router'
import { useAppStore } from '@/stores/app'

const props = defineProps({
  product: {
    type: Object,
    required: true
  }
})

const router = useRouter()
const appStore = useAppStore()

const truncateText = (text, maxLength) => {
  if (!text) return ''
  return text.length > maxLength ? text.substring(0, maxLength) + '...' : text
}

const goToDetail = () => {
  router.push(`/products/${props.product.id}`)
}

const contactNow = () => {
  const whatsapp = appStore.storeInfo.whatsapp
  if (whatsapp) {
    const message = `Hi, I'm interested in ${props.product.name_en}`
    const url = `https://wa.me/${whatsapp.replace(/\s/g, '')}?text=${encodeURIComponent(message)}`
    window.open(url, '_blank')
  }
}
</script>

<style lang="scss" scoped>
.product-card {
  background: var(--color-white);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  transition: var(--transition-base);
  cursor: pointer;
  height: 100%;
  display: flex;
  flex-direction: column;
  
  &:hover {
    transform: translateY(-8px);
    box-shadow: var(--shadow-lg);
  }
}

.product-image {
  position: relative;
  width: 100%;
  padding-top: 100%; // 1:1 aspect ratio
  overflow: hidden;
  
  :deep(.el-image) {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
  
  .image-placeholder {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--color-gray-light);
  }
}

.hot-badge {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  background: linear-gradient(135deg, #FF6B6B, #FF4757);
  color: white;
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 0.25rem;
  box-shadow: var(--shadow-md);
}

.product-info {
  padding: 1rem;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.product-name {
  font-size: 1.125rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-description {
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin-bottom: 0.75rem;
  flex: 1;
  line-height: 1.5;
}

.product-colors {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
  
  .color-dot {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 2px solid white;
    box-shadow: 0 0 0 1px var(--color-gray);
  }
  
  .more-colors {
    font-size: 0.75rem;
    color: var(--text-secondary);
  }
}

.product-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 0.75rem;
  border-top: 1px solid var(--color-gray-light);
}

.product-category {
  font-size: 0.875rem;
  color: var(--color-primary);
  font-weight: 500;
}

.contact-btn {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.5rem 1rem;
  background: linear-gradient(135deg, var(--color-primary), var(--color-secondary));
  color: white;
  border: none;
  border-radius: var(--radius-md);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition-fast);
  
  &:hover {
    transform: scale(1.05);
    box-shadow: var(--shadow-md);
  }
}
</style>

